/* 
This do file calculates the state-level weighted average value of generation given current installations in each zip.
NOTES: This requires "genval_severin" from Borenstein and Bushnell, which we obtained by permission from those authors. It is replaced with zeros in the "Generation_and_damages_withCO2_2021-03-05_eGrid_SR.csv" file here.

All directories and paths should be set in the main "SetUpSubsidyCalcs.do" file.
*/

clear all

import delimited `"${data_path}/Generation_and_damages_withCO2_2021-03-05_eGrid_SR.csv"'

rename v7 inter
rename zip zipcode

merge 1:1 zipcode using `"${data_intermediate}/NumberSystems.dta"'
*Note: We have 1478 zipcodes with solar panels but no damage calculations.
*So I assign damages to these zipcodes based on the damages from the previous zipcode (when zipcodes are listed in numerical order)
replace genval_severin = genval_severin[_n-1] if _merge==2
drop _merge

*drop Puerto Rico, Hawaii, and Alaska
drop if state=="HI" | state=="AK" | state=="PR"

gen zip_total_genval = num_systems*genval_severin

sort state
bysort state: egen state_num_systems = sum(num_systems)
bysort state: egen state_total_genval = sum(zip_total_genval)

gen state_wtd_avg_genval = state_total_genval/state_num_systems

collapse (mean) state_wtd_avg_genval, by(state)

*outsheet state state_wtd_avg_genval using `"${data_intermediate}/state_wtd_avg_genval.csv"', comma replace
export excel using `"${scripts_subsidies}/Solar_Incentives_Data_no_severin_data.xlsx"', sheet("Annual_Wholesale_Value_4kWsyste") sheetmodify cell(A2) keepcellfmt
